package com.cby.android.sample.adapter

import android.widget.ImageView
import android.widget.LinearLayout
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.cby.android.olive.ext.dp
import com.cby.android.olive.ext.getScreenWidth
import com.cby.android.sample.R
import com.cby.android.sample.bean.MetaImageBean
import com.cby.android.sample.constant.MetaConstant
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * @description: RollMoreAdapter
 *
 * @author ChenBaoyang
 * @date 4/23/21 11:07
 */
class SquareListAdapter : BaseQuickAdapter<MetaImageBean, BaseViewHolder>(R.layout.item_meta_image) {

    override fun convert(holder: BaseViewHolder, item: MetaImageBean) {
        val image = holder.getView<ImageView>(R.id.iv_meta_image)
        //updateSize(item, currentType)
        val options = RequestOptions().apply {
            fitCenter()
            placeholder(R.drawable.loading_bixue)
            error(R.drawable.loading_bixue)
            diskCacheStrategy(DiskCacheStrategy.ALL)
        }
        Glide.with(context).load(item.img).apply(options).into(image)

    }

    fun updateSize(bean: MetaImageBean, type: Int, itemView: LinearLayout, image: ImageView) {
        if(type == MetaConstant.META_IMAGE_TYPE_STAGE) {
            val width = itemView.context.getScreenWidth/2 - 20.dp
            //进行原图片宽高比，进行计算实际空间高度，增加一个随机高度差
            val offset = (Math.random() * 100)
            val height = (width * bean.height) / bean.width
            image.layoutParams = LinearLayout.LayoutParams(width,height)
        }
    }
}